// pages/addProduct/classifys/classify.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [
      { value: '1', name: '时尚', checked: 'true'},
      { value: '2', name: '美妆' },
      { value: '3', name: '母婴' },
      { value: '4', name: '书籍' },
      { value: '5', name: '居家' },
      { value: '6', name: '数码' },
      { value: '-1', name: '其他' },
    ],
    inputHidden:false,
    leibie:'时尚',
    leibieId:1,
    typeID:'0'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      typeID: options.typeID,
    })
    var items = this.data.items;
    var leibieId = 0
    for (var i = 0, len = items.length; i < len; ++i) {
      if (items[i].value == options.leibieId) {
        items[i].checked = true
        leibieId = i + 1
      } else {
        items[i].checked = false
      }
    }
    if (leibieId != 0) {
      this.setData({
        items: items,
        leibieId: leibieId,
        leibie: items[leibieId - 1].name
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function (res) {
    var title = this.data.shareMsg
    var path = "pages/home/home?userId=" + getApp().globalData.userId
    return {
      title: title,
      path: path,
      success: function (res) {
      },
      fail: function (res) {
      }
    }
  },
  
  selfClassify:function(e){
    this.setData({ leibie: e.detail.value})
  },
  radioChange: function (e) {
    var that = this
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    if (e.detail.value == "其他"){
      that.setData({inputHidden:false})
    }else{
      that.setData({ inputHidden: true})
    }
    if (e.detail.value == -1){
      that.setData({ leibie: '其他' })
    }else{
      that.setData({ leibie: that.data.items[e.detail.value - 1].name })
    }
    that.setData({ leibieId: e.detail.value })
    var items = this.data.items;
    for (var i = 0, len = items.length; i < len; ++i) {
      if (items[i].value == e.detail.value){
        items[i].checked = true
      }else{
        items[i].checked = false
      }
    }
    this.setData({
      items: items
    });


  },
  submitForm:function(e){

    // 获取表单id
    let formId = e.detail.formId;
    getApp().saveUserFormId(formId, 16);
    console.log("formId-16", formId)
  },
  querenClick:function(e){
    if (this.data.leibieId == 0) {
      this.showTost('请选择分类！', 'none');
        return
    }
    var arr = getCurrentPages();
    var that = this
    var controllerFrom = ''
    if (this.data.typeID == '0'){
      controllerFrom = 'pages/addProduct/publishDetails/publishDetail'
    } else if (this.data.typeID == '1'){
      controllerFrom = 'pages/addProduct/compileProduct/compipleProduct'
    }
    if (arr[arr.length - 2].route == controllerFrom) {
      wx.navigateBack({
        delta: 1,
        success: function (res) {
          var leiBie = that.data.leibie
          arr[arr.length - 2].setData({ leibie: leiBie })
          arr[arr.length - 2].setData({ leibieId: that.data.leibieId })
        },
        fail: function () {

        },
        complete: function () {

        }
      })
    }
  },
    showTost: function (str, icon) {
    wx.showToast({
      title: str,
      icon: icon,
      duration: 1000,
      mask: true
    })
  },

})